<%-- 
    Document   : postingProcess
    Created on : Apr 24, 2012, 11:25:41 AM
    Author     : Robert
--%>
<%@page import="javax.swing.text.Document"%>
<!DOCTYPE html>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>  
<%@ page import="java.util.List"%>  
<%@ page import="java.util.Iterator"%>  
<%@ page import="java.io.File"%>  
<%@ page import="org.apache.commons.fileupload.*"%>  
<%@ page import="org.apache.commons.fileupload.disk.*"%>  
<%@ page import="org.apache.commons.fileupload.servlet.*"%>  
  
<html>  
<head>  
<title>Commons-FileUpload-1.1 Report Page</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
  
<body>  

<%!	  
	//method to return file extension  
	String getFileExt(String xPath){  
  
            //Find extension  

            int dotindex = 0;	//extension character position  
            dotindex = xPath.lastIndexOf('.');  

            if (dotindex == -1){	// no extension	  
                return "";  
            }  

            int slashindex = 0;	//seperator character position  
            slashindex = Math.max(xPath.lastIndexOf('/'),xPath.lastIndexOf('\\'));  

            if (slashindex == -1){	// no seperator characters in string  
                return xPath.substring(dotindex);  
            }  

            if (dotindex < slashindex){	//check last "." character is not before last seperator  
                return "";  
            }  
            return xPath.substring(dotindex);  
	}

        
%>  
<%  
	// Check that we have a file upload request  
	boolean isMultipart = FileUpload.isMultipartContent(request);  
	out.println("isMultipart="+isMultipart+"<br>");  
	  
	//Create variables for path, filename and extension  
	String newFilePath = application.getRealPath("/") + "images";  
	String newFileName ="";  
	String FileExt = "";  
	  
	// Create a factory for disk-based file items  
	FileItemFactory factory = new DiskFileItemFactory();  
	  
	// Create a new file upload handler  
	ServletFileUpload upload = new ServletFileUpload(factory);  
	  
	// Parse the request  
	List /* FileItem */ items = upload.parseRequest(request);  
	  
	// Process the uploaded items  
	Iterator iter = items.iterator();  
	//Form fields   
	while (iter.hasNext()) {  
            FileItem item = (FileItem) iter.next();  
	  
            if (item.isFormField()) {  
                String name = item.getFieldName();  
                String value = item.getString();  
                if (name.equals("newFileName")) {  
                        newFileName = value;  
                }  
                out.println("Form Field Name= " + name + "<br>");  
                out.println("Form Field Value= " + value + "<br><br>");  
            }   
            else {  
                String fieldName = item.getFieldName();  
                String fileName = item.getName();  
                FileExt = getFileExt(fileName);  
                String contentType = item.getContentType(); 
                // check file extension 
                if (!contentType.equals("image/jpeg") ){
                    out.println("Ekstensi File Salah");
                    response.sendRedirect("testUpload.jsp");
                }
                
                boolean isInMemory = item.isInMemory();  
                long sizeInBytes = item.getSize();  
                out.println("fieldName= " + fieldName + "<br>");  
                out.println("fileName= " + fileName + "<br>");  
                out.println("FileExt= " + FileExt + "<br>");  
                out.println("contentType= " + contentType + "<br>");  
                out.println("isInMemory= " + isInMemory + "<br>");  
                out.println("sizeInBytes= " + sizeInBytes + "<br>");
                out.println("path  = "+ newFilePath + "<br>");
                if (fileName.equals("") || sizeInBytes==0){  
                        out.println("Not a valid file.<br>No upload attempted.<br><br>");  
                } else {  
                    File uploadedFile = new File(newFilePath+"/", newFileName+FileExt);  
                    try{  
                            //item.write(uploadedFile);  
                            out.println(fileName+" was successfully uploaded as "+ newFileName+FileExt +".<br><br>");  
                    }  
                    catch (java.lang.Exception e) {  
                            out.println("Errors prevented the file upload.<br>"+fileName+ " was not uploaded.<br><br>");  
                    }  
                }  
            }  
	}

    
        String redirectURL = "Profile.php";
        //response.sendRedirect(redirectURL);
              
%>  

</body>
</html>
